package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMeal_DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper flavorMapper;
    @Autowired
    private SetMeal_DishMapper setMeal_dishMapper;

    /**
     * 新增菜品
     *
     * @param dto
     */
    @Transactional
    @Override
    public void insert(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        // 插入菜品
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        // 插入口味
        List<DishFlavor> flavors = dto.getFlavors();
        if(flavors == null) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishId);
            });
            flavorMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dto) {
        // 分页插件初始化
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        // 分页查询
        Page<DishVO> page = dishMapper.list(dto);


        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        // 检查是不是起售中的菜品
        ids.forEach(id -> {
            Dish dish = dishMapper.selectById(id);
            if (Objects.equals(dish.getStatus(), StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        // 检查是否被套餐关联
        ids.forEach(id -> {
            // 获取表中dish_id = id的值, count > 0 的话,
            Integer count = setMeal_dishMapper.getCountByDishId(id);
            if (count > 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        });
        // 删除菜品
        dishMapper.deleteByIds(ids);
        // 删除菜品相关的口味
        flavorMapper.deleteByIds(ids);
    }

    @Override
    public Result<DishVO> getDishById(Long id) {
        DishVO dishVO = new DishVO();
        // 根据id查询菜品
        Dish dish = dishMapper.selectById(id);
        BeanUtils.copyProperties(dish, dishVO);
        // 根据菜品id查询对应口味
        List<DishFlavor> flavor = flavorMapper.getById(id);
        dishVO.setFlavors(flavor);
        return Result.success(dishVO);
    }

    @Transactional // 操作两张表, 开启事务
    public void update(DishDTO dto) {
        // 修改菜品
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto, dish);
        dishMapper.updateDish(dish);
        // 修改口味
        // 1. 删除口味
        flavorMapper.getById(dto.getId());
        // 2. 新增口味
        List<DishFlavor> flavors = dto.getFlavors();
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish.getId());
            });
            flavorMapper.insertBatch(flavors);
        }
    }


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.listBy(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = flavorMapper.getById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        dishMapper.updateStatus(status, id);
    }
}
